﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AOP.Filter.Jurisdiction;
using CacheService.Standard;
using Common.Standard;
using System.ComponentModel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Admin.Models;
using AOP.Filter.Login;

namespace Admin.Controllers.Public
{
    [Route("api/[controller]/[action]")]
    [ApiController, NoJurisdiction]
    public class PublicApiController : ControllerBase
    {
        ICache cache;
        public PublicApiController(ICache cache)
        {
            this.cache = cache;
        }
        /// <summary>
        /// 获取
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public List<Jurisdict> GetJurisdicts()
        {
            var list = cache.get<List<Jurisdict>>("actions");
            if (list != null)
            {
                return list;
            }
            //获取所有需要验证的控制器
            var types = AssemblyHelper.GetAssembly("Admin").GetTypes().AsParallel().Where(x => x.BaseType.Name.Contains("Controller") && !x.IsDefined(typeof(NoJurisdictionAttribute), true)).ToList();
            list = new List<Jurisdict>();
            foreach (Type type in types)
            {
                var des = type.GetCustomAttributes(typeof(DescriptionAttribute), true);
                var groupName = des.Count() > 0 ? ((DescriptionAttribute)des[0]).Description : type.Namespace.Split(".").LastOrDefault();
                var jur = list.Where(x => x.groupName == groupName).FirstOrDefault();
                if (jur == null)
                {
                    jur = new Jurisdict();
                    jur.groupName = groupName;
                    list.Add(jur);
                }
                var actions = type.GetMethods().AsParallel().Where(x => x.Module.ScopeName.ToLower().StartsWith("admin")).ToList();
                var view = new jurisdictView();

                foreach (var act in actions)
                {
                    var names = act.GetCustomAttributes(typeof(DescriptionAttribute), true);
                    var path = $"/{type.Name.Replace("Controller", "")}/{act.Name}";
                    var name = names.Count() > 0 ? ((DescriptionAttribute)names[0]).Description : path;
                    if (act.ReturnType.Name == "IActionResult" && string.IsNullOrWhiteSpace(view.viewName))
                    {
                        view.viewName = name;
                        view.path = path;
                    }
                    //查重

                    if (view.actionApis.AsParallel().Where(x => x.name == name).Count() > 0)
                    {
                        continue;
                    }
                    view.actionApis.Add(new actionPath
                    {
                        name = name,
                    });
                }
                view.actionApis = view.actionApis.Distinct().ToList();
                jur.jurisdictViews.Add(view);
            }
            cache.setWithLasting("actions", list);
            return list;
        }
        [HttpGet]
        public List<Jurisdict> GetPages()
        {
            var list = cache.get<List<Jurisdict>>("views");
            if (list != null)
            {
                return list;
            }
            //获取所有需要验证的控制器
            var types = AssemblyHelper.GetAssembly("Admin").GetTypes().AsParallel().Where(x => x.BaseType.Name.Contains("Controller") && !x.IsDefined(typeof(NoJurisdictionAttribute), true)).ToList();
            list = new List<Jurisdict>();
            foreach (Type type in types)
            {
                var des = type.GetCustomAttributes(typeof(DescriptionAttribute), true);
                var groupName = des.Count() > 0 ? ((DescriptionAttribute)des[0]).Description : type.Namespace.Split(".").LastOrDefault();
                var jur = list.Where(x => x.groupName == groupName).FirstOrDefault();
                if (jur == null)
                {
                    jur = new Jurisdict();
                    jur.groupName = groupName;
                    list.Add(jur);
                }
                var actions = type.GetMethods().AsParallel().Where(x => x.Module.ScopeName.ToLower().StartsWith("admin") && x.ReturnType.Name == "IActionResult").ToList();
                var view = new jurisdictView();

                foreach (var act in actions)
                {
                    var names = act.GetCustomAttributes(typeof(DescriptionAttribute), true);
                    var path = $"/{type.Name.Replace("Controller", "")}/{act.Name}";
                    var name = names.Count() > 0 ? ((DescriptionAttribute)names[0]).Description : path;
                    
                    if (act.ReturnType.Name == "IActionResult")
                    {
                        view.actionApis.Add(new actionPath
                        {
                            name = name,
                            path = path
                        });
                    }
                }
                if (view.actionApis.Count > 0)
                {
                    jur.jurisdictViews.Add(view);
                }
            }
            cache.setWithLasting("views", list);
            return list.Where(x => x.jurisdictViews.Count > 0).ToList();
        }
    }
}